package com.tlgen.controller.student;

import com.tlgen.model.*;
import com.tlgen.service.AvatarFileService;
import com.tlgen.service.InstituteService;
import com.tlgen.service.RegistryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author: tlgen
 * @Discription:
 */
@RestController
@RequestMapping("/student/registry")
public class RegistryController {

    @Autowired
    RegistryService registryService;

    @Autowired
    InstituteService instituteService;

    @Autowired
    AvatarFileService avatarFileService;

    @GetMapping("/")
    public RespPageBean getRegistryByPage(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer size,
                                        Registry registry) {
        return registryService.getRegistryByPage(page, size, registry);
    }

    @PostMapping("/")
    public RespBean addRegistry(@RequestBody Registry registry) {
        if (registryService.addRegistry(registry) == 1) {
            return RespBean.ok("添加成功!");
        }
        return RespBean.error("添加失败!!");
    }

    @GetMapping("/institutes")
    public List<Institute> getAllInstitutes() {
        return instituteService.getAllInstitutes();
    }

    @DeleteMapping("/{id}")
    public RespBean deleteRegistryByEid(@PathVariable Integer id) {
        if (registryService.deleteRegistryById(id) == 1) {
            return RespBean.ok("删除成功!");
        }
        return RespBean.error("删除失败!");
    }

    @PutMapping("/")
    public RespBean updateRegistry(@RequestBody Registry registry) {
        if (registryService.updateRegistry(registry) == 1) {
            return RespBean.ok("更新成功!");
        }
        return RespBean.error("更新失败!");
    }

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");

    @PostMapping("/uploadAvatar")
    public RespBean uploadAvatar(MultipartFile file, HttpServletRequest request) throws IOException {
        String format = simpleDateFormat.format(new Date());
        String realPath = request.getServletContext().getRealPath("/upload") + format;
        File folder = new File(realPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String oldName = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
        file.transferTo(new File(folder, newName));
        String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/upload"
                + format + newName;
        System.out.println(url);
        AvatarFile avatarFile = new AvatarFile();
        avatarFile.setUrl(url);
        avatarFileService.addFile(avatarFile);
        return RespBean.ok("上传成功!");
    }

}
